package com.jetwinner.webfast.module.bigapp.datatag;

import com.jetwinner.util.EasyStringUtil;
import com.jetwinner.util.FastHashMap;
import com.jetwinner.util.ValueParser;
import com.jetwinner.webfast.kernel.dao.support.OrderBy;
import com.jetwinner.webfast.kernel.datatag.BaseDataFetcher;
import com.jetwinner.webfast.kernel.datatag.annotations.FastDataTag;
import com.jetwinner.webfast.kernel.exception.RuntimeGoingException;
import com.jetwinner.webfast.module.bigapp.service.AppGroupService;
import org.springframework.context.ApplicationContext;

import java.util.List;
import java.util.Map;

/**
 * @author xulixin
 */
@FastDataTag
public class HotGroupDataFetcher extends BaseDataFetcher {

    private final AppGroupService groupService;

    public HotGroupDataFetcher(ApplicationContext applicationContext) {
        super(applicationContext);
        this.groupService = applicationContext.getBean(AppGroupService.class);
    }

    /**
     * 获取最热小组
     *
     * 可传入的参数：
     *
     *   count 必需 必需 小组数量，取值不能超过100
     *
     * @param  arguments 参数
     * @return array 最热小组
     */
    @Override
    public List<Map<String, Object>> getData(Map<String, Object> arguments) {
        List<Map<String, Object>> hotGroups;
        if (EasyStringUtil.isBlank(arguments.get("count"))) {
            throw new RuntimeGoingException("count参数缺失");
        } else {
            hotGroups = this.groupService.searchGroups(FastHashMap.build(1).add("status", "open").toMap(),
                    OrderBy.build(1).addDesc("memberNum"),
                    0,
                    ValueParser.parseInt(arguments.get("count")));
        }
        return hotGroups;
    }
}
